package com.stfalcon.chatkit.sample.features.demo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.widget.AppCompatButton;

import com.google.gson.Gson;
import com.stfalcon.chatkit.sample.R;
import com.stfalcon.chatkit.sample.common.data.model.Dialog;
import com.stfalcon.chatkit.sample.common.data.ReceiveData;
import com.stfalcon.chatkit.sample.features.demo.def.DefaultMessagesActivity;
import com.stfalcon.chatkit.sample.task.AsyncRquest;
import com.stfalcon.chatkit.sample.url.URLConstant;
import com.stfalcon.chatkit.sample.utils.SharedPreferencesAtrribute;

import java.net.URLEncoder;
import java.util.Locale;


public class LoginActivity extends DemoDialogsActivity {

    private static final String TAG = "LoginActivity";


    AppCompatButton loginApp;

    ProgressBar googleSignLoader;

    public static void open(Context context) {
        context.startActivity(new Intent(context, LoginActivity.class));
    }


    @Override
    protected void onStart() {
        //Check if user is logged
        super.onStart();
        SharedPreferences preferences = getSharedPreferences("lang_settings", Activity.MODE_PRIVATE);
        String language = preferences.getString("lang","");
        setLocale(language);
    }



    private void setLocale(String lang) {

        Locale locale = new Locale(lang);
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        config.locale = locale;

        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);


        this.loginApp = this.findViewById(R.id.login_app);

        this.checkLogin();

        this.loginApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View var1){
                login();
            }
        });
    }

    /**
     * 是否登录
     */
    private void checkLogin()
    {
        String token = SharedPreferencesAtrribute.getSharedPreferences().getString("token", "");

        if (token.equals("") || 0 == token.length()) {
            return;
        }

        try {
            AsyncRquest task = new AsyncRquest(URLConstant.IS_LOGIN,"token="+token);

            AsyncTask<Object, Object, String> res = task.execute();
            String receive = res.get();
            Gson son = new Gson();
            ReceiveData<String> user = son.fromJson(receive, ReceiveData.class);
            if (0 == user.getStatus()) {
                Toast.makeText(LoginActivity.this, user.getMessage(), Toast.LENGTH_SHORT).show();
                return;
            }
            this.jumpMessage();

        } catch (Exception e) {
            Log.i("验证登录", e.getMessage());
        }
    }

    public void onDialogClick(Dialog dialog) {
        DefaultMessagesActivity.open(this);
    }


    private void displayToast(String s) {

        Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
    }

    protected void login()
    {


        EditText userName = findViewById(R.id.username);

        EditText password = findViewById(R.id.passowrd);


        String value = null;
        try {
            value = URLEncoder.encode("user_name", "UTF-8") + "=" +
                    URLEncoder.encode(userName.getText().toString(), "UTF-8") + "&" +
                    URLEncoder.encode("password", "UTF-8") + "=" +
                    URLEncoder.encode(password.getText().toString(), "UTF-8");
//            new Thread(() -> {
                AsyncRquest task = new AsyncRquest(URLConstant.LOGIN, value);

                AsyncTask<Object, Object, String> res =  task.execute();

                String receive = res.get();
                Gson son = new Gson();
                ReceiveData<String> user = son.fromJson(receive, ReceiveData.class);
                if (0 == user.getStatus()) {
                    Toast.makeText(LoginActivity.this, user.getMessage(), Toast.LENGTH_SHORT).show();
                    return;
                }
                System.out.println("token:"+ user.getData());

                SharedPreferences.Editor editor =  SharedPreferencesAtrribute.getSharedPreferences().edit();

                editor.putString("token", (String) user.getData());

                editor.apply();
                jumpMessage();
//            }).run();
        }  catch (Exception e) {
            Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            return;
        }

        System.out.println(password.getText().toString());
    }

    protected void jumpMessage()
    {
        Intent content = new Intent();
//
        content.setClass(LoginActivity.this, FrameActivity.class);


        startActivity(content);
    }
}